/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2013-2019.
 * Description: header for topp feature
 * Author: heshaoliang
 * Create: 2013-6-15
 */

#ifndef RTOS_LINUX_CPUTIME_MSA_H
#define RTOS_LINUX_CPUTIME_MSA_H

#ifdef CONFIG_RTOS_MICROSTATE_ACCT
#include <linux/time.h>
#include <linux/jiffies.h>
#include <linux/msa.h>
#include <linux/sched/clock.h>

#ifndef CONFIG_X86
#include <linux/math64.h>
#endif

typedef u64 msatime64_t;
typedef u64 msatime_t;

#define msatime64_add(__a, __b)         ((__a) + (__b))
#define msatime_to_msatime64(__ct)      ((u64) __ct)

#ifdef CONFIG_X86
#define MSA_NOW(now) do { (now) = rdtsc_ordered(); } while (0)
#else
#if defined(CONFIG_RTOS_MICROSTATE_ACCT_TSC_CLOCKSOURCE)
/*
 * Use the processor's time-stamp counter as a timesource
 */
#include <asm/msr.h>

#define MSA_NOW(now)  do { (now) = rdtsc_ordered(); } while (0)

#elif defined(CONFIG_RTOS_MICROSTATE_ACCT_SCHED_CLOCK_CLOCKSOURCE)
#define MSA_NOW(now)  do { (now) = sched_clock(); } while (0)
#else
#error "No clocksource defined for Microstate Accounting"
#endif /* CONFIG_RTOS_MICROSTATE_ACCT_TSC_CLOCKSOURCE */
#endif /* CONFIG_X86 */

#ifdef CONFIG_RTOS_THREAD_TIME_HSTAT
extern int task_cputime_hstat_get(struct task_struct *task, msa_time_t *ut, msa_time_t *st);
extern int thread_group_cputime_hstat_get(struct task_struct *task, msa_time_t *ut, msa_time_t *st,
					  msa_time_t *cut, msa_time_t *cst);
#endif

#endif /* CONFIG_RTOS_MICROSTATE_ACCT */
#endif /* RTOS_LINUX_CPUTIME_MSA_H */
